/*
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
 * 02111-1307, USA.
 *
 * http://www.gnu.org/copyleft/gpl.html
 */

package l1j.jrwz.server.clientpackets;

import java.util.logging.Level;
import java.util.logging.Logger;

import l1j.jrwz.server.ClientThread;
import l1j.jrwz.server.model.L1Character;
import l1j.jrwz.server.model.L1World;
import l1j.jrwz.server.model.Instance.L1PcInstance;
import l1j.jrwz.server.model.Instance.L1PetInstance;
import l1j.jrwz.server.model.Instance.L1SummonInstance;
import l1j.jrwz.server.serverpackets.S_ServerMessage;

// Referenced classes of package l1j.jrwz.server.clientpackets:
// ClientBasePacket

/**
 * 處理收到由客戶端傳來選擇目標的封包
 */
public class C_SelectTarget extends ClientBasePacket {

    private static final String C_SELECT_TARGET = "[C] C_SelectTarget";

    private static Logger _log = Logger.getLogger(C_SelectTarget.class
            .getName());

    @Override
    public void execute(byte[] decrypt, ClientThread client) {
        try {
            read(decrypt);

            int petId = readD();
            @SuppressWarnings("unused")
            int type = readC();
            int targetId = readD();

            L1PetInstance pet = (L1PetInstance) L1World.getInstance()
                    .findObject(petId);
            L1Character target = (L1Character) L1World.getInstance()
                    .findObject(targetId);

            if (pet != null && target != null) {
                // 目标为玩家
                if (target instanceof L1PcInstance) {
                    final L1PcInstance pc = (L1PcInstance) target;
                    // 目标在安全区域、攻击者在安全区域、NOPVP
                    if ((pc.getZoneType() == 1) || (pet.getZoneType() == 1)
                            || (pc.checkNonPvP(pc, pet))) {
                        // 宠物主人
                        if (pet.getMaster() instanceof L1PcInstance) {
                            final L1PcInstance petMaster = (L1PcInstance) pet
                                    .getMaster();
                            petMaster.sendPackets(new S_ServerMessage(328)); // 请选择正确的对象。
                        }
                        return;
                    }
                    if (pc == pet.getMaster()) {
                        return;
                    }
                }
                // 目标为宠物
                else if (target instanceof L1PetInstance) {
                    final L1PetInstance targetPet = (L1PetInstance) target;
                    // 目标在安全区域、攻击者在安全区域
                    if ((targetPet.getZoneType() == 1)
                            || (pet.getZoneType() == 1)) {
                        // 宠物主人
                        if (pet.getMaster() instanceof L1PcInstance) {
                            final L1PcInstance petMaster = (L1PcInstance) pet
                                    .getMaster();
                            petMaster.sendPackets(new S_ServerMessage(328)); // 请选择正确的对象。
                        }
                        return;
                    }
                }
                // 目标为召唤怪
                else if (target instanceof L1SummonInstance) {
                    final L1SummonInstance targetSummon = (L1SummonInstance) target;
                    // 目标在安全区域、攻击者在安全区域
                    if ((targetSummon.getZoneType() == 1)
                            || (pet.getZoneType() == 1)) {
                        // 宠物主人
                        if (pet.getMaster() instanceof L1PcInstance) {
                            final L1PcInstance petMaster = (L1PcInstance) pet
                                    .getMaster();
                            petMaster.sendPackets(new S_ServerMessage(328)); // 请选择正确的对象。
                        }
                        return;
                    }
                }
                pet.setMasterTarget(target);
            }
        } catch (final Exception e) {
            _log.log(Level.SEVERE, e.getLocalizedMessage(), e);
        } finally {
            finish();
        }
    }

    @Override
    public String getType() {
        return C_SELECT_TARGET;
    }
}
